package com.lineage.server.timecontroller.quest;

import com.lineage.data.quest.ADLv80_3;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Location;
import com.lineage.server.serverpackets.S_PacketBoxGree;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1QuestUser;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.utils.L1SpawnUtil;
import com.lineage.server.world.WorldQuest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class AD80_3_Timer extends TimerTask {
  private static final Log _log = LogFactory.getLog(AD80_3_Timer.class);
  
  private static final Random _random = new Random();
  
  private ScheduledFuture<?> _timer;
  
  private int _qid = -1;
  
  private int _counter;
  
  private L1Location _loc;
  
  public void start() {
    this._qid = ADLv80_3.QUEST.get_id();
    this._loc = new L1Location(32848, 32877, 1017);
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, 2000L, 2000L);
  }
  
  public void run() {
    try {
      ArrayList<?> questList = WorldQuest.get().getQuests(this._qid);
      if (questList.isEmpty())
        return; 
      Object[] array;
      int length = (array = questList.toArray()).length;
      int i = 0;
      while (i < length) {
        Object object = array[i];
        L1QuestUser quest = (L1QuestUser)object;
        Iterator<L1NpcInstance> iterator = quest.npcList().iterator();
        while (iterator.hasNext()) {
          L1NpcInstance npc = iterator.next();
          if (!npc.isDead()) {
            L1Location loc = npc.getLocation().randomLocation(12, true);
            if (npc.getNpcId() >= 97204 && npc.getNpcId() <= 97209 && ++this._counter >= 45) {
              this._counter = 0;
              boolean check_spawn = false;
              Iterator<?> localIterator2 = quest.npcList().iterator();
              while (localIterator2.hasNext()) {
                L1NpcInstance check_npc = (L1NpcInstance)localIterator2.next();
                if (check_npc.getNpcId() == 97210) {
                  check_spawn = true;
                  break;
                } 
              } 
              if (!check_spawn) {
                loc = this._loc.randomLocation(12, true);
                L1SpawnUtil.spawn(97210, loc, 0, quest.get_id());
              } 
            } 
            if (npc.getNpcId() < 97204 || npc.getNpcId() > 97206 || 
              _random.nextInt(100) >= 5)
              continue; 
            Iterator<L1PcInstance> iterator2 = quest.pcList().iterator();
            while (iterator2.hasNext()) {
              L1PcInstance tgpc = iterator2.next();
              switch (_random.nextInt(3)) {
                case 0:
                  npc.getNpcTemplate().set_weakAttr(4);
                  tgpc.sendPackets((ServerBasePacket)new S_PacketBoxGree(6));
                  break;
                case 1:
                  npc.getNpcTemplate().set_weakAttr(1);
                  tgpc.sendPackets((ServerBasePacket)new S_PacketBoxGree(7));
                  break;
                case 2:
                  npc.getNpcTemplate().set_weakAttr(8);
                  tgpc.sendPackets((ServerBasePacket)new S_PacketBoxGree(8));
                  break;
              } 
            } 
          } 
        } 
        i++;
      } 
      questList.clear();
    } catch (Exception e) {
      _log.error("风龙副本 伤害计能施放 时间轴异常重启", e);
      GeneralThreadPool.get().cancel(this._timer, false);
      AD80_3_Timer ad80_3Timer = new AD80_3_Timer();
      ad80_3Timer.start();
    } 
  }
}
